package com.zoctan.api.common.aspect;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * controller日志切面
 *
 * @author Zoctan
 * @date 2018/07/13
 */
@Slf4j
@Component
public class RequestInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    log.debug(">>>>>>>>>>>>>>>  {}", request.getRequestURI());
    log.debug("---- RequestParams: {}", ToStringBuilder.reflectionToString(request.getParameterMap()));
    return true;
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
      ModelAndView modelAndView) throws Exception {
    log.debug("postHandle==> ModelMap: {}", modelAndView);
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    log.debug("postHandle==> Exception: {}", ex);
    log.debug("<<<<<<<<<<<<<<<   {}", request.getRequestURI());
  }
}
